package com.ksd.api.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksd.api.bean.SynchApkRequest;
import com.ksd.api.bean.SynchApkResponse;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.SysVersion;
import com.ksd.system.service.ISysVersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class checkVersion extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(checkVersion.class);
    @Autowired
    private ISysVersionService sysVersionService;

    @RequestMapping(value = "/api/cloud/synchApkVersion", method = RequestMethod.POST)
    @ResponseBody
    public String checkApkVersion(HttpServletRequest httpServletRequest) {
        System.out.println("===================播控后台管理系统同步机顶盒版本信息===============");
        StringBuffer requestURL = httpServletRequest.getRequestURL();

        SynchApkResponse response = new SynchApkResponse();
        String req = HttpServletUtil.getReqByInputStream(httpServletRequest);
        ObjectMapper mapper = new ObjectMapper();
        SynchApkRequest request = null;
        try {
            request = mapper.readValue(req, SynchApkRequest.class);
            if (StringUtils.isNotNull(request)) {
                String operatorKey = request.getOperatorKey();
                System.out.println("待同步升级的机顶盒运营商标识:"+operatorKey);
                SysVersion sysVersion = new SysVersion();
                sysVersion.setOperatorKey(operatorKey);
                List<SysVersion> sysVersions = sysVersionService.selectSysVersionList(sysVersion);
                System.out.println("待升级同步的机顶盒:"+sysVersions.size());
                if (sysVersions.size() > 0) {
                    sysVersion = sysVersions.get(0);
                    String url = sysVersion.getUrl();
                    String operatorName = sysVersion.getOperatorName();
                    String version = sysVersion.getVersion();
                    if (StringUtils.isNotEmpty(operatorName) && StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(version)) {
                        System.out.println("查询到云服务上该运营商有需要升级的机顶盒:"+url);
                        response.setApkName(sysVersion.getApkName());
                        response.setApkPath(sysVersion.getUrl());
                        response.setApkVersion(sysVersion.getVersion());
                        return response.setSuccess(response);
                    }
                    return response.setError("该运营商机顶盒在云服务器上暂时没有版本,请先在云服务上上传升级包");
                }else{
                    return response.setError("该运营商机顶盒在云服务器上暂时还未注册,请确认运营商:"+operatorKey);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return response.setError(e.getMessage());
        }
        return response.setError("error");
    }
}